using BuildingBlocks.Presentation.Endpoints;
using BuildingBlocks.Presentation.Results;
using Mediator;

namespace Catalog.Api.Features.CatalogItems.Create;

internal sealed class CreateCatalogItemEndpoint : IEndpoint
{
    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapPost(
                Routes.Base, async
                (CreateCatalogItemRequest request,
                    IMediator mediator,
                    CancellationToken cancellationToken) =>
                {
                    var command = new CreateCatalogItemCommand(
                        request.Name,
                        request.Description,
                        request.Price,
                        request.Stock,
                        request.PictureUrl,
                        request.Category);

                    var result = await mediator.Send(command, cancellationToken);
                    return result.Match(Results.Ok, ApiResponse.Problem);
                })
            .WithName("CreateCatalogItem")
            .WithTags("Catalog");
        //.RequireAuthorization();
    }
}
